﻿using System;
using System.Data.Objects;
using System.Linq;
using Colligate;

namespace OrgDataSource
{
// ReSharper disable InconsistentNaming
    public class ApplicationDS:IOrgDS
// ReSharper restore InconsistentNaming
    {
        #region\\delete
        int IOrgDS.DeleteTaskData(OUDataInfo userInfo, string dataGuid, string dataType, orgEntities context)
        {
            switch (dataType.ToLower())
            {
                case "application": return DeleteApplication(dataGuid, context);
                default: throw new Exception("ApplicationDS无法处理该" + dataType + "的数据");
            }
        }

        protected int DeleteApplication(string dataGuid, orgEntities context)
        {
            try
            {
                var application = context.Application.FirstOrDefault(cc => cc.Guid == dataGuid);
                context.DeleteObject(application);
                var result = context.SaveChanges();
                return result == 1 ? 0 : 1;
            }
            catch
            {
                throw new Exception("删除数据标识为：" + dataGuid + "的Application数据时出错！");
            }
        }
        #endregion
        #region\\get
        System.Xml.XmlNode IOrgDS.GetTaskData(OUDataInfo userInfo, string dataGuid, string dataType, orgEntities context, BaseTaskDataSource baseInfo)
        {
            switch (dataType.ToLower())
            {
                case "application": return GetApplication(dataGuid, context);
                case "applicationlist": return GetApplicationList(dataGuid, context);
                case "apptasklist": return GetAppTaskList(dataGuid, context);
                default: throw new Exception("ApplicationDS无法处理该" + dataType + "的数据");
            }
        }

        private static System.Xml.XmlNode GetApplication(string dataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "Application");
                var application = context.V_Application.FirstOrDefault(cc => cc.Guid == dataGuid) ?? new V_Application
                                                                                                         {
                                                                                                             Guid = string.Empty,
                                                                                                             Name = string.Empty,
                                                                                                             IPAddress = string.Empty,
                                                                                                             Protocol = string.Empty,
                                                                                                             CompanyGuid = string.Empty,
                                                                                                             CompanyName = string.Empty,
                                                                                                             Admin = string.Empty,
                                                                                                             Telephone = string.Empty,
                                                                                                             EMail = string.Empty,
                                                                                                             MasterType = string.Empty,
                                                                                                             SubType = string.Empty,
                                                                                                             PlatGuid=string.Empty
                                                                                                         };
                XmlNodeManage.SetSubValue(result, "Guid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.Guid);
                XmlNodeManage.SetSubValue(result, "Name", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.Name);
                XmlNodeManage.SetSubValue(result, "IPAddress", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.IPAddress);
                XmlNodeManage.SetSubValue(result, "Protocol", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.Protocol);
                XmlNodeManage.SetSubValue(result, "CompanyGuid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.CompanyGuid);
                XmlNodeManage.SetSubValue(result, "CompanyName", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.CompanyName);
                XmlNodeManage.SetSubValue(result, "Admin", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.Admin);
                XmlNodeManage.SetSubValue(result, "Telephone", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.Telephone);
                XmlNodeManage.SetSubValue(result, "EMail", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.EMail);
                XmlNodeManage.SetSubValue(result, "MasterType", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.MasterType);
                XmlNodeManage.SetSubValue(result, "SubType", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), application.SubType);
                return result;
            }
            catch
            {
                throw new Exception("获取数据标识为：" + dataGuid + "的Application数据时出错！");
            }
        }
        private static System.Xml.XmlNode GetApplicationList(string dataGuid, orgEntities context)
        {
            try
            {
                if (dataGuid != "e6d49c36-b387-409e-a8fe-9fc752177501")
                    throw new Exception("获取的ApplicationList数据时出错！数据标识不正确！");
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "ApplicationList");
                var cloneList = new Application[context.Application.Count()];
                context.Application.ToList().CopyTo(cloneList);
                var tempList = context.Application.Where(cc => cc.PlatGuid != "").GroupBy(cc => cc.MasterType, cc => cc.SubType);
                //ObjectQuery<System.Data.Common.DbDataRecord> query = context.Application.GroupBy("it.MasterType,it.SubType", "it.MasterType,it.SubType");
                foreach (var r in tempList)
                {
                    var masterType = r.Key;
                    var subType = r.ToList()[0];
                    var subNode = XmlNodeManage.CreateNode("ListOUDataInfo");
                    XmlNodeManage.SetSubValue(subNode, "@MasterType", masterType);
                    XmlNodeManage.SetSubValue(subNode, "@SubType", subType);
                    var temp = cloneList.Where(cc => cc.MasterType == masterType && cc.SubType == subType);
                    foreach (var item in temp)
                    {
                        var childNode = XmlNodeManage.CreateNode("OUDataInfo");
                        XmlNodeManage.SetSubValue(childNode, "@GUID", item.Guid);
                        XmlNodeManage.SetSubValue(childNode, "@NAME", item.Name);
                        XmlNodeManage.SetSubValue(childNode, "@OUType", "Application");
                        XmlNodeManage.SetSubValue(childNode, "@CompanyGuid", item.CompanyGuid);
                        subNode.AppendChild(childNode);
                    }
                    result.AppendChild(subNode);
                }
                return result;
            }
            catch
            {
                throw new Exception("获取的ApplicationList数据时出错！");
            }
        }
        private static System.Xml.XmlNode GetAppTaskList(string dataGuid, orgEntities context)
        {
            try
            {
                var result = XmlNodeManage.GetDocumentNode(Utility.GetTaskGuid(), dataGuid, "AppTaskList");
                var taskList = context.WorkTask.Where(cc => cc.AppGuid == dataGuid && cc.Status == "true").OrderBy(cc => cc.TaskType).OrderBy(cc=>cc.Sort);
                foreach (var item in taskList)
                {
                    var subNode = XmlNodeManage.CreateNode("AppTaskList");
                    XmlNodeManage.SetSubValue(subNode, "Guid", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), item.Guid);
                    XmlNodeManage.SetSubValue(subNode, "Name", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), item.Name);
                    XmlNodeManage.SetSubValue(subNode, "Status", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), item.Status);
                    XmlNodeManage.SetSubValue(subNode, "TaskType", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), item.TaskType);
                    XmlNodeManage.SetSubValue(subNode, "Icon", Enum.GetName(typeof(TypeEnum), TypeEnum.TEXT), item.Icon);
                    XmlNodeManage.SetSubValue(subNode, "Order", Enum.GetName(typeof(TypeEnum), TypeEnum.LONG), item.Sort);
                    result.AppendChild(subNode);
                }
                return result;
            }
            catch
            {
                throw new Exception("获取的AppTaskList数据时出错！");
            }
        }
        #endregion
        #region\\set
        int IOrgDS.SetTaskData(OUDataInfo userInfo, string dataGuid, string dataType, System.Xml.XmlNode xmlData, orgEntities context)
        {
            var datatype = XmlNodeManage.GetSubValue(xmlData, "@DataType");
            switch (datatype.ToLower())
            {
                case "application": return SetApplication(xmlData, context);
                default: throw new Exception("ApplicationDS无法处理该" + datatype + "的数据");
            }
        }

        private static int SetApplication(System.Xml.XmlNode xmlData, orgEntities context)
        {
            try
            {
                var id = Utility.CheckXmlData(xmlData,"Guid");
                var application = context.Application.FirstOrDefault(cc => cc.Guid == id);
                if (null == application)
                {
                    application = new Application
                                      {
                                          Guid = id,
                                          Name = Utility.CheckXmlData(xmlData, "Name"),
                                          IPAddress = Utility.CheckXmlData(xmlData, "IPAddress"),
                                          Protocol = Utility.CheckXmlData(xmlData, "Protocol"),
                                          CompanyGuid = Utility.CheckXmlData(xmlData, "CompanyGuid"),
                                          Admin = Utility.CheckXmlData(xmlData, "Admin"),
                                          Telephone = Utility.CheckXmlData(xmlData, "Telephone"),
                                          EMail = Utility.CheckXmlData(xmlData, "EMail"),
                                          MasterType = Utility.CheckXmlData(xmlData, "MasterType"),
                                          SubType = Utility.CheckXmlData(xmlData, "SubType")
                                      };
                    context.AddToApplication(application);
                }
                else
                {
                    application.Name = Utility.CheckXmlData(xmlData, "Name");
                    application.IPAddress = Utility.CheckXmlData(xmlData, "IPAddress");
                    application.Protocol = Utility.CheckXmlData(xmlData, "Protocol");
                    application.CompanyGuid = Utility.CheckXmlData(xmlData, "CompanyGuid");
                    application.Admin = Utility.CheckXmlData(xmlData, "Admin");
                    application.Telephone = Utility.CheckXmlData(xmlData, "Telephone");
                    application.EMail = Utility.CheckXmlData(xmlData, "EMail");
                    application.MasterType = Utility.CheckXmlData(xmlData, "MasterType");
                    application.SubType = Utility.CheckXmlData(xmlData, "SubType");
                }
                var result = context.SaveChanges();
                return result == 1 ? 0 : 1;
            }
            catch
            {
                throw new Exception("写入application数据时出错！");
            }
        }
        #endregion
        #region\\transform
        System.Xml.XmlNode IOrgDS.TransformTaskData(OUDataInfo userInfo, System.Xml.XmlNode xmlParam, orgEntities context, BaseTaskDataSource baseInfo)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
